package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author atguigu-mqx
 * @ClassName VipStrategy
 * @description: TODO
 * @date 2025年03月10日
 * @version: 1.0
 */
@Service("1003")
@Slf4j
public class VipStrategy implements ItemTypeStrategy {

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //  声明一个变量;
        int result = 0;
        if (SystemConstant.ORDER_ITEM_TYPE_VIP.equals(userPaidRecordVo.getItemType())) {
            //  购买vip; user_vip_service; user_info;
            //  考虑购买时长;续期; user_vip_service.id;
            Long vipServiceConfigId = userPaidRecordVo.getItemIdList().get(0);
            //  获取购买时间;
            VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServiceConfigId);
            Integer serviceMonth = vipServiceConfig.getServiceMonth();
            UserVipService userVipService = new UserVipService();
            //  续期的条件; 现在是未过期的vip!
            //  获取信息;
            UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
            Date vipExpireTime = new Date();
            //  是未过期的vip 属于续期;
            if (1 == userInfo.getIsVip() && userInfo.getVipExpireTime().after(new Date())) {
                //  续期; 在原有过期时间基础上+现在购买时间;
                vipExpireTime = userInfo.getVipExpireTime();
                //  日期计算; jdk8有自带的工具类;
                LocalDateTime localDateTime = new LocalDateTime(vipExpireTime).plusMonths(serviceMonth);
//                localDateTime.plusMonths(serviceMonth);
                //  设置过期时间;
                //  userVipService.setExpireTime(localDateTime.toDate());
                vipExpireTime = localDateTime.toDate();
            } else {
                //  当前系统时间+购买时间;
//                userVipService.setExpireTime(new LocalDateTime(userVipService).plusMonths(serviceMonth).toDate());
                vipExpireTime = new LocalDateTime(vipExpireTime).plusMonths(serviceMonth).toDate();
            }
            userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
            userVipService.setUserId(userPaidRecordVo.getUserId());
            userVipService.setStartTime(new Date());
            //  设置过期时间
            userVipService.setExpireTime(vipExpireTime);
            //  保存数据;
            result += userVipServiceMapper.insert(userVipService);
            //  抛出异常！
            //  int i = 1/0;
            //  更新user_info;
            //  不管你是是不是续期，还是购买
            userInfo.setIsVip(1);
            userInfo.setVipExpireTime(vipExpireTime);
            result += userInfoMapper.updateById(userInfo);
        }
        //  返回数据;
        return result;
    }
}
